<?php
// +----------------------------------------------------------------------
// | Copyright © Sogu Technologies Co. Ltd 2018-2050 All rights reserved.
// +----------------------------------------------------------------------
// | File name: BankCard.php
// +----------------------------------------------------------------------
// | Author: liuju
// +----------------------------------------------------------------------
// | Description: 描述文档功能
// +----------------------------------------------------------------------
// | Others: 其他内容的说明
// +----------------------------------------------------------------------
// | History: 修改历史记录列表，每条修改记录应包含修改日期、修改者及修改内容简述
// +----------------------------------------------------------------------
// | 1.Date: 2019-02-28 Author: liuju Modification: 创建
// +----------------------------------------------------------------------

namespace App\ModelTransfers\Traits;


use App\Exceptions\GeneralException;
use App\Models\User;
use App\Models\UserBankCard;
use App\ModelTransfers\WsAccount\PayBindCard;
use Illuminate\Support\Facades\DB;

trait BankCard
{

    /**
     * Description:移除迁移银行卡
     * Author: liuju
     * DateTime: 2019-03-05 17:29
     * @param $userId
     * @return bool|int|null
     * @throws \Exception
     */
    public  static  function  removeTransfer($userId = '')
    {
      return UserBankCard::whereNotNull('old_kskd_user_id')
          ->when($userId,function($query) use($userId){
              return $query->where('uid',$userId);
          })
          ->delete();
    }



    /**
     * Description: 迁移用户银行卡数据
     * Author: liuju
     * DateTime: 2019-02-28 14:19
     * @param $tUser
     * @param $userId
     * @return bool
     */
    public static function transfer($tUser, $userId)
    {
        $data =  PayBindCard::select([
            DB::raw($userId . ' as uid'),
            'user_id as old_kskd_user_id',
            'card_ower as card_username',
            'card_no as card_num',
            'aside_bank_phone as phone_num',
            'bank_name as bank_name',
            DB::raw('if(bank_deposit,bank_deposit,"") as bank_name_show'),
            'bank_code',
            DB::raw('(case card_type when 1 then 1 when 0 then 2 else 3 end) as is_credit'),
            DB::raw('if(pay_bind_id,pay_bind_id,"") as hold_agreement_num'),
            'create_date as  created_at',
            'update_date  as updated_at',
        ])->where(['is_valid' => 1])
            ->where(function($query) use ($tUser){
                $query->where(['user_id' => $tUser->id])->orWhere(function ($query) use($tUser){
                    $query->where(['aside_bank_phone'=>$tUser->phone]);
                });
            })->get()->toArray();

        $cardList = UserBankCard::where(function($query) use ($userId,$tUser){
            return  $query->where('uid',$userId)->orWhere('phone_num',$tUser->phone_num);
        })->pluck('card_num')->toArray();
        $newData = [];
        foreach($data as $key=>$item) {
            if(in_array($item['card_num'],$cardList)){
                continue;
            }
            $item['is_valid'] = UserBankCard::VALID_NO;
            $newData [] = $item;
        }
        $newData && DB::table(UserBankCard::getModel()->getTable())->insert($newData);
        $num = $tUser->bindCard ? $tUser->bindCard->bank_card_no:'';
        if($num){
            $cardId = UserBankCard::where(['card_num'=>$num])->where(function($query) use ($userId,$tUser){
                return $query->where('uid',$userId)->orWhere('phone_num',$tUser->phone);
            })->value('id');
            if($cardId){
                User::where(['id'=>$userId])->update(['using_bank_card' => $cardId]);
            }else{
              throw new GeneralException('「用户'.$tUser->id.'有使用的银行卡号码'.$num.'但是未能关联上」');
            }

        }
    }
}